define(["cmn","jquery","lib/render","diqye/cookie","diqye/md5"],function(cmn,$,a,cookie,md5){
	var FN_MAP={},ejs=cmn.ejs("19911204/19911204home");
	ejs.set("main","main");
	ejs.set("login","login");
	ejs.set("detail","detail");
	cmn.post({action:"functionMap"}).then(function(data){
		FN_MAP=data;
	})
	return {
		init:function(){
			this.code="000";
			this.renderMain();
		},
		renderMain:function(){
			var _=this,code,moudel={
				action:"User_imsg"
			};
			
			cmn.post(moudel).then(function(data){

				_.el.html(ejs.main,data,function(){
					cmn.loginData=data;
					//点击菜单
					_.el.find("#menulist [code]").on("click",function(){
						if($(this).parent("li").is(".active"))return;
						code=$(this).attr("code");
						// if("000"==code){
						// 	_.renderMain();
						// 	return;
						// }
						//加载controller
						_.loadController(code);
						_.el.find("#menulist li").removeClass("active");
						$(this).parent("li").addClass("active");
					});
					//登录
					_.el.find("#login1013").on("click",function(){
						_.renderLogin();
					});
					//退出
					_.el.find("#loginout").on("click",function(){
						_.renderLoginout();
					});
					//我的信息
					_.el.find("#myself1027").on("click",function(){
						_.renderDetail();
					});
					_.loadController("000");
				});

				//如果没有登录
				if(!data.login&&cookie.get("ua")!=null){
					var model={
								action:"User_login",
								params:{
									user_account:cookie.get("ua"),
									user_password:cookie.get("up")
								}
							};
							cmn.lbox.show();
							cmn.post(model).then(function(data){
								cmn.lbox.hide();
								_.renderMain();
							},function(){cookie.remove("ua");});
				}
			});
			
		},
		loadController:function(code){
			var _=this,container=_.el.find("#container");
			if(code in FN_MAP){
				cmn.lbox.show();
				require([FN_MAP[code]],function(controller){
					controller.el=container;
					controller.code=code;
					cmn.lbox.hide();
					controller.init();
				});
			}
		},
		renderLogin:function(){
			var _=this,formObj;
			_.el.html(ejs.login,function(){
				_.el.find("#login-reset").on("click",function(){
					_.el.reset();
				});
				_.el.find("#login-login").on("click",function(){
					if(_.el.validate()){
						formObj=_.el.formToObject();
						var model={
							action:"User_login",
							params:formObj
						};
						cmn.lbox.show();
						cmn.post(model).then(function(data){
							cmn.lbox.hide();
							cookie.add("ua",formObj.user_account,12*7);
-							cookie.add("up",formObj.user_password,12*7);
							_.renderMain();
						});
					}
				});
				_.el.find("#login-return").on("click",function(){
					_.renderMain();
				});
				_.el.find("[name=user_password]").on("keyup",function(e){
					if(e.keyCode==13){
						_.el.find("#login-login").trigger("click");
					}
				});
			});
		},
		renderLoginout:function(){
			var _=this;
			cmn.lbox.show();
			cmn.post({action:"User_loginout"}).then(function(data){
				cmn.lbox.hide();
				cookie.remove("ua");
				_.renderMain();
			});
		},
		renderDetail:function(){
			var _=this,model;

			model={
				action:"User_queryOne",
				params:{
					"qq":cmn.loginData.qq
				}
			};
			cmn.post(model).then(function(data){
				_.el.find("#container").append(ejs.detail,data,function(){
					_.el.find("#detail1027").modal().modal("show").on("hide.bs.modal",function(){
						_.el.find("#detail1027").remove();
					});
				});
			});
		}
	}
});